package com.wechat.core.service;

import com.wechat.core.util.TokenManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hgb
 * @date 2019/1/27
 * Email xhy18650@sina.com
 */
public abstract class AbstractService {

    protected Logger logger = LoggerFactory.getLogger(AbstractService.class);

    protected Map<String, Object> getParams() {
        Map<String, Object> params = new HashMap<>();
        params.put("access_token", TokenManager.getAccessToken());
        logger.debug("access_token===>>{}",TokenManager.getAccessToken());
        return params;
    }

    protected File saveDisk(MultipartFile media) throws IOException {
        String path = "src/main/resources/tmp/" + media.getOriginalFilename();
        InputStream initialStream = media.getInputStream();
        byte[] buffer = new byte[initialStream.available()];
        initialStream.read(buffer);
        File targetFile = new File(path);
        OutputStream outStream = new FileOutputStream(targetFile);
        outStream.write(buffer);
        return targetFile;
    }

}
